//
//  QsbkOnlyContextViewModel.m
//  NewProject
//
//  Created by Livespro on 2017/4/27.
//  Copyright © 2017年 FZ. All rights reserved.
//

#import "QsbkOnlyContextViewModel.h"

@interface QsbkOnlyContextViewModel ()

@property (nonatomic,   copy)  NSString *pageSign;//页数

@end

@implementation QsbkOnlyContextViewModel

- (NSMutableArray *)contextDataArray{
    
    if (!_contextDataArray) {
        
        _contextDataArray = [NSMutableArray array];
    }
    return _contextDataArray;
}

/**
 请求糗事百科数据

 @param result resultBlock
 */
- (void)qsbkOnlyContextType:(BaseRefreshType)refreshType Module:(NSString *)moduleName Result:(void(^)(BOOL isSuccess,NSString *desc))result{
    
    if (refreshType == RefreshTypeRenew) {//刷新
        
        _pageSign = @"1";
        [self.contextDataArray removeAllObjects];
        
    } else {//加载
        
        if (!_pageSign) {
            _pageSign = @"0";
        }
        
        _pageSign = [NSString stringWithFormat:@"%ld",_pageSign.integerValue + 1];
    }
    
    __weak typeof(self) weakSelf = self;
    [self autoCancelRequestWith:QSBK(moduleName)];
    [NetworkCentre Request:QSBK(moduleName) Type: isQSBKCache ? NetworkRequestTypeGetCache : NetworkRequestTypeGet parameters:@{@"count" : QSBKPageShow,@"page" : _pageSign} success:^(id responseObject) {
        
        NSDictionary *dic = responseObject;//.err
        
        if (![dic[@"err"] integerValue]) {
            
            NSArray *dataArr = dic[@"items"];
            for (NSDictionary *oneDic in dataArr) {
                
                QsbkOnlyContextModel *model = [QsbkOnlyContextModel yy_modelWithDictionary:oneDic];
                
                [weakSelf planModelLayout:model];
                
                [weakSelf.contextDataArray addObject:model];
                
            }
            result(YES,@"");
        }else{
            
            result(NO,@"获取数据失败");
        }
        
    } failure:^(NSError *error) {
        
        result(NO,@"网络错误");
    }];
}

- (void)planModelLayout:(QsbkOnlyContextModel *)model{
    
//    dispatch_async(dispatch_get_global_queue(0, 0), ^{
    
        model.contentHeight = [model.content boundingRectWithSize:CGSizeMake(kScreenWidth - 20, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:kQsbkContentFont]} context:nil].size.height;
        
        if ([model.format isEqualToString:@"image"]) {
            
            NSString *widthImg = model.image_size[@"s"][0];
            NSString *heightImg = model.image_size[@"s"][1];
            
            model.imageHeight = (kScreenWidth - 20) / widthImg.floatValue * heightImg.floatValue;
        }
        
        if ([model.format isEqualToString:@"video"]) {
            
            NSString *widthImg = model.pic_size[0];
            NSString *heightImg = model.pic_size[1];
            
            model.imageHeight = (kScreenWidth - 20) / widthImg.floatValue * heightImg.floatValue;
        }
        
        model.cellHeight =  kQsbkHeaderHeight + (model.contentHeight ? (model.contentHeight + 10) : model.contentHeight) + (model.imageHeight ? (model.imageHeight +10) : model.imageHeight);
        
//    });
    
}

@end
